package com.chpro.http.yixin.menu;

import com.chpro.common.HttpUtil;
import com.chpro.common.JSONUtils;
import com.chpro.http.yixin.servlet.ExtendActionServlet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定议菜单
 * Created by Administrator on 14-10-2.
 */
public class MenuService {
    /**
     * 创建菜单
     */
    public void createMuenu()throws Exception{
        List button = new ArrayList();  //一级菜单

        Map menu1 = new HashMap();
        menu1.put("name","业务办理");
        List<Button> buttonList1 = new ArrayList<Button>();
        buttonList1.add(new Button("/ywbl/hfcz.yx","话费充值","click"));
        buttonList1.add(new Button("/ywbl/sdmjf.yx","水电煤缴费","click"));
        buttonList1.add(new Button("/ywbl/hmzz.yx","号码转帐","click"));
        buttonList1.add(new Button("/ywbl/jytx.yx","开通/取消交易","click"));
        menu1.put("sub_button",buttonList1);

        Map menu2 = new HashMap();
        menu2.put("name","我");
        List<Button> buttonList2 = new ArrayList<Button>();
        buttonList2.add(new Button("/w/yzfkh.yx","翼支付开户","click"));
        buttonList2.add(new Button("/w/bdwdyzf.yx","绑定我的翼支付","click"));
        buttonList2.add(new Button("/w/yecx.yx","余额查询","click"));
        menu2.put("sub_button",buttonList2);

        Map menu3 = new HashMap();
        menu3.put("name","精彩小翼");
        List<Button> buttonList3 = new ArrayList<Button>();
        buttonList3.add(new Button("/jcxy/gyyzf.yx","关于翼支付","click"));
        buttonList3.add(new Button("/jcxy/yhsh.yx","优惠生活","click"));
        buttonList3.add(new Button("/jcxy/znxy.yx","智能小翼","click"));
        menu3.put("sub_button",buttonList3);

        button.add(menu1);
        button.add(menu2);
        button.add(menu3);

        Map oneMenu = new HashMap();
        oneMenu.put("button",button);

        ExtendActionServlet extendActionServlet = new ExtendActionServlet();
        extendActionServlet.init(); //加载配置文件
        String content = JSONUtils.toJSON(oneMenu);
        String url = ExtendActionServlet.commonConfig.getString("menu_create_url")+"?access_token="+ExtendActionServlet.getYixinToKen("access_token");
        System.out.println(url);
        String result = HttpUtil.httpsJsonPost(url,content);
        System.out.println(result);
    }

    /**
     * 清空菜单
     */
    public void clearMenu()throws Exception{
        ExtendActionServlet extendActionServlet = new ExtendActionServlet();
        extendActionServlet.init(); //加载配置文件
        String url = ExtendActionServlet.commonConfig.getString("menu_delete_url")+"?access_token="+ExtendActionServlet.getYixinToKen("access_token");
        System.out.println(url);
        String result = HttpUtil.httpsGet(url);
        System.out.println(result);
    }

    public static void main(String[] args)throws Exception{
        MenuService menuService = new MenuService();
        menuService.clearMenu();  //删除菜单
        menuService.createMuenu();  //创建菜单
    }
}
